import _pretty from 'pretty'
export let pretty = _pretty

export function getVer() {
  return '1.0.0'
}

export function beautifyHtml(v) {
  let _html = window.html_beautify(v)
  return _html.replace(/(<img[^>]+>)/g, function (...args) {
    return`  ${args[1].trim().replace('>', ' />')}            
`
  })
}

export let webviewRunCode = (webview, code) => {
  return new Promise((resolve) => {
    webview.executeJavaScript(
      code
    );

    webview.contentWindow.postMessage("Send me your data!", "*");
    window.addEventListener("message", receiveHandshake, false); // Listen for response

    function receiveHandshake(event) {
      // Data is accessible as event.data.*
      // This is the custom object that was injected during contentload()
      // i.e. event.data.title, event.data.url
      // console.log(event.data)
      window.removeEventListener("message", receiveHandshake);
      resolve(event.data)
    }
  })
}

export function makeid(length) {
  var result = ''
  var characters =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  var charactersLength = characters.length
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength))
  }
  return result
}
